package com.wjjun.base.widget.status

import android.view.LayoutInflater
import android.view.View
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import com.wjjun.base.BR
import com.wjjun.base.ui.listener.IItemViewListener
import com.wjjun.base.ui.viewmodel.BaseViewModel
import com.wjjun.base.ui.viewmodel.BaseWindowViewModel
import com.wjjun.base.widget.status.viewmodel.LoadingPagerViewModel
import com.wjjun.base.widget.status.viewmodel.StatusPagerViewModel

/**
 *
 * 状态控件助手
 * 默认加载界面、错误界面、权限界面、成功界面
 * 加载：0
 * 空界面：100
 * 成功：200
 * 权限错误：300
 * 错误：400
 * 自定义请设置值大于1000, 点击事件请使用onItemViewModel
 * author : wangjunjun
 * date   : 2022/6/20
 * desc   :
 */
class StatusViewHelper() : StatusListPagerView.StatusPagerAdapter, IItemViewListener {

    private var statusListView: StatusListPagerView? = null
    var listener: StatusViewHelperListener? = null
    private var lifecycleOwner: LifecycleOwner? = null

    fun setLifeCleOwner(lifecycleOwner: LifecycleOwner) {
        this.lifecycleOwner = lifecycleOwner
        lifecycleOwner.lifecycle.addObserver(object : LifecycleObserver {
            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
            fun onDestroy() {
                statusListView = null
                listener = null
            }
        })
    }

    fun setStatsView(viewList: StatusListPagerView) {
        statusListView = viewList
        statusListView?.setAdapter(this)
        initStatus()
    }

    private fun initStatus() {
        setLoadingViewModel(LoadingPagerViewModel())
        setEmptyViewModel(StatusPagerViewModel(Status.EMPTY).apply {
            msg.set("空界面")
        })
        setPermissionViewModel(StatusPagerViewModel(Status.PERMISSION).apply {
            msg.set("请求权限界面")
        })
        setErrorViewModel(StatusPagerViewModel(Status.ERROR).apply {
            msg.set("请求错误界面")
        })
    }

    /**
     * 设置加载界面
     */
    fun setLoadingViewModel(viewModel: StatusPagerViewModel) {
        addStatusViewModel(Status.LOADING, viewModel)
    }

    /**
     * 设置空界面
     */
    fun setEmptyViewModel(viewModel: StatusPagerViewModel) {
        addStatusViewModel(Status.EMPTY, viewModel)
    }

    fun setEmpty(icon: Int, msg: String) {
        statusListView?.getStatusViewModel(Status.EMPTY)?.apply {
            if (this is StatusPagerViewModel) {
                this.icon.set(icon)
                this.msg.set(msg)
            }
        }
    }

    /**
     * 设置成功界面
     */
    fun setSuccessViewModel(viewModel: BaseWindowViewModel) {
        addStatusViewModel(Status.SUCCESS, viewModel)
    }


    /**
     * 设置网络错误界面
     */
    fun setErrorViewModel(viewModel: StatusPagerViewModel) {
        addStatusViewModel(Status.ERROR, viewModel)
    }

    fun setError(icon: Int, msg: String) {
        statusListView?.getStatusViewModel(Status.ERROR)?.apply {
            if (this is StatusPagerViewModel) {
                this.icon.set(icon)
                this.msg.set(msg)
            }
        }
    }

    /**
     * 设置权限错误
     */
    fun setPermissionViewModel(viewModel: BaseWindowViewModel) {
        addStatusViewModel(Status.PERMISSION, viewModel)
    }

    fun setPermission(icon: Int, msg: String) {
        statusListView?.getStatusViewModel(Status.PERMISSION)?.apply {
            if (this is StatusPagerViewModel) {
                this.icon.set(icon)
                this.msg.set(msg)
            }
        }
    }


    /**
     * 添加自定义状态
     */
    fun addStatusViewModel(status: Status, viewModel: BaseWindowViewModel) {
        if (viewModel is StatusPagerViewModel) {
            statusListView?.setStatusView(viewModel.status, viewModel)
        } else {
            statusListView?.setStatusView(status, viewModel)
        }
    }

    fun showLoading() {
        setStatus(Status.LOADING)
    }

    fun showEmpty() {
        setStatus(Status.EMPTY)
    }

    fun showSuccess() {
        setStatus(Status.SUCCESS)
    }

    fun showError() {
        setStatus(Status.ERROR)
    }


    fun showPermissionError() {
        setStatus(Status.PERMISSION)
    }

    /**
     * 设置状态
     */
    fun setStatus(status: Status) {
        statusListView?.setStatus(status)
    }

    override fun getView(view: View, status: Status, viewModel: BaseWindowViewModel): View {
        val viewDataBinding = DataBindingUtil.inflate<ViewDataBinding>(
                LayoutInflater.from(view.context),
                viewModel.getLayoutId(),
                null,
                false
        )
        viewDataBinding.lifecycleOwner = lifecycleOwner

        when (status) {
            Status.LOADING, Status.EMPTY, Status.ERROR, Status.PERMISSION -> {
                viewDataBinding.setVariable(BR.viewModel, viewModel)
                viewDataBinding.setVariable(BR.listener, this)
            }
            else -> {
                listener?.bindViews(viewDataBinding, status, viewModel)
            }
        }

        return viewDataBinding.root
    }

    override fun onItemClick(view: View, itemViewModel: BaseViewModel) {
        when (itemViewModel) {
            is StatusPagerViewModel -> {
                listener?.onStatusPageClick(view, itemViewModel)

            }
        }
    }

    interface StatusViewHelperListener {
        fun bindViews(viewDataBinding: ViewDataBinding, status: Status, viewModel: BaseWindowViewModel)
        fun onStatusPageClick(view: View, viewModel: StatusPagerViewModel)
    }


}